package p2p.peer.command;

import p2p.logic.connection.Command;
import p2p.logic.connection.client.Client;
import p2p.logic.connection.communication.message.Message;
import p2p.logic.connection.communication.message.CommandVisitor;
import p2p.logic.log.Logger;
import p2p.peer.ClientManager;
import p2p.peer.command.visitor.GSVisitor;

public class Spawn extends Command {
	
	private Message message;
	
	private boolean isAck;
	
	public Spawn(Message m, CommandVisitor v) {
		super(v);
	}
	
	public Spawn(boolean isSpawn){
		
		this.isAck = isSpawn;
		
	}
	
	public Spawn(CommandVisitor v, Message m, boolean isAck){
		this.visitor = v;
		this.message = m;
		this.isAck = isAck;
	}

	public int getCommandCode() {

		return 0;
	}

	@Override
	public void execute() {
		
		Client c;
		
		if (!isAck) 
			c = new Client(this.message.getSenderIp(), this.message.getSenderPort());
		else
			c = new Client(this.message.getRecipientIp(), this.message.getRecipientPort());
		
		ClientManager manager = ((GSVisitor) getVisitor()).getManager();
		
		manager.addClient(c);
		Logger.consoleLog("Neighborlist: " + manager.getPeer().getId() + ": " + manager.toString());
	}
	

	
}
